<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // let p2 = new Promise((resolve,reject)=>{});
    // console.log(p2);

    // 自定义MyPromise类
    class MyPromise {
      constructor(handle) {  // handle----->(resolve,reject)=>{}
        // 状态；
        console.log(this);  // 不要关注this的定义，需要关注this的调用
        this['[[PromiseState]]'] = "pendding";
        this['[[PromiseResult]]'] = undefined;
        handle(function (val) {
          // 外部的resolve函数
          console.log(this);
          this['[[PromiseState]]'] = "fulfilled";
          this['[[PromiseResult]]'] = val;
        }.bind(this), function (err) {
          // 外部的reject函数
          console.log(this);
          this['[[PromiseState]]'] = "rejected";
          this['[[PromiseResult]]'] = err;
          throw new Error(err)
        }.bind(this)); // 调用外部传入的回调函数
      }
    }

    let p = new MyPromise((resolve, reject) => {
      // resolve("success");
      reject("error")
    })
    console.log(p);
  </script>
</body>

</html>